#define _CRT_SECURE_NO_WARNINGS
#include"contact.h"
#include<assert.h>
#include<stdlib.h>
void InitContact(struct Contact* ps)
{
	memset(ps->data, 0, sizeof(ps->data));
	ps->count = 0; //contact inital state is zreo
}

void AddContact(struct Contact* ps)
{
	if (ps->count==MAX)
	{
		printf("contact is full");
	}
	else
	{
	printf("please input name:>\n");
	scanf("%s",ps->data[ps->count].name);
	printf("please input age:>\n");
	scanf("%d", &(ps->data[ps->count].age));
	printf("please input sex:>\n");
	scanf("%s", ps->data[ps->count].sex);
	printf("please input telephone:>\n");
	scanf("%s", ps->data[ps->count].tele);
	printf("please input address:>\n");
	scanf("%s", ps->data[ps->count].addr);
	}
	ps->count++;
	printf("succeed\n");
}

static int FindByName(struct Contact* ps, char name[MAX_NAME])
{
	assert(ps);
	int i = 0;
	for ( i = 0; i < ps->count; i++)
	{
		if (0 == strcmp (ps->data[i].name, name))
		{
			return i;
		}
	}
	return -1;
}
void DeleteContact(struct Contact* ps)
{
	char name[MAX_NAME] = { 0 };
	assert(ps);
	if (ps->count==0)
	{
		printf("no contacts need to delete");
	}
	else
	{
		printf("please input the name of contact you want to delete");
		scanf("%s", name);
		int pos = FindByName(ps, name);
		if (pos == -1)
		{
			printf("the name of contact you want to delete is inexistence\n");
		}
		else
		{
			int i = 0;
			for ( i = pos; i <ps->count-1; i++)
			{
				ps->data[i] = ps->data[i = 1];
			}
			ps->count--;
			printf("successfully delete\n");
		}


	}
}
void ShowContact(struct Contact* ps)
{
	if (ps->count == 0)
	{
		printf("no contacts to show");
	}
	else
	{
		int i = 0;
		printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n",
			"name","age","sex", "telephone", "address");
		for (i = 0; i < ps->count; i++)
		{
			printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
				ps->data[i].name,
				ps->data[i].age,
				ps->data[i].sex,
				ps->data[i].tele,
				ps->data[i].addr);
		}
	}
	
}
void SearchContact(struct Contact* ps)
{
	char name[MAX_NAME] = { 0 };
	assert(ps);
	if (ps->count == 0)
	{
		printf("no contact to search");
	}
	else
	{
		int pos = FindByName(ps, name);
		if (pos == -1)
		{
			printf("the name of contact you want to search is inexistence\n");
			return;
		}
		else
		{
			printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n",
				"name", "age", "sex", "telephone", "address");
			printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
				ps->data[pos].name,
				ps->data[pos].age,
				ps->data[pos].sex,
				ps->data[pos].tele,
				ps->data[pos].addr);
		}
		
	}
}
void ModifyContact(struct Contact* ps)
{
	char name[MAX_NAME] = { 0 };
	assert(ps);
	if (ps->count == 0)
	{
		printf("no contact to modify");
	}
	else
	{
		int pos = FindByName(ps, name);
		if (pos == -1)
		{
			printf("the name of contact you want to modify is inexistence\n");
			return;
		}
		else
		{
			printf("start modifying\n");
			printf("please input name:>\n");
			scanf("%s", ps->data[pos].name);
			printf("please input age:>\n");
			scanf("%d", ps->data[pos].age);
			printf("please input sex:>\n");
			scanf("%s", ps->data[pos].sex);
			printf("please input telephone:>\n");
			scanf("%s", ps->data[pos].tele);
			printf("please input address:>\n");
			scanf("%s", ps->data[pos].addr);
		}

	}
}
int cmp_poe_by_name(const void* e1, const void* e2)
{
	return strcmp(((struct PeoInfo*)e1)->name, ((struct PeoInfo*)e2)->name);
}
void SortContact(struct Contact* ps)
{
	assert(ps);
	if (ps->count == 0)
	{
		printf("no contact to sort");
	}
	else
	{
		qsort(ps->data,ps->count,sizeof(struct PeoInfo), cmp_poe_by_name);
		printf("successfully sort\n");
	}

}